﻿/**
<code>
  <author name="Zealic" email="zealic(at)gmail.com" type="Creator"/>
</code>
**/
using System;
using System.ComponentModel;


namespace Zealib.Transform
{
    /// <summary>
    /// TypeConverter transformer adapter
    /// </summary>
    internal sealed class TypeConverterTransformer : ITransformer
    {
        public TypeConverterTransformer(TypeConverter converter)
        {
            if (converter == null) throw new ArgumentNullException("converter");
            Converter = converter;
        }

        public TypeConverter Converter
        {
            get;
            private set;
        }

        #region ITransformer Members
        public bool CanTransformTo<TDestiation>()
        {
            return Converter.CanConvertTo(typeof(TDestiation));
        }

        public bool CanTransformBack<TSource>()
        {
            return Converter.CanConvertTo(typeof(TSource));
        }

        public TDestiation TransformTo<TDestiation>(object source)
        {
            return (TDestiation)Converter.ConvertTo(source, typeof(TDestiation));
        }

        public TSource TransformBack<TSource>(object destiation)
        {
            return (TSource)Converter.ConvertFrom(destiation);
        }

        #endregion
    }

}
